/*
Copyright (c) 2009, Dan Hagerstrand and contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Cirrus Creative nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef BENG_LAYER_H
#define BENG_LAYER_H

#include <bitset>

#include <list>

#include "Array2d.h"
#include "BengFixedPoint.h"
#include "BengInterface.h"
#include "BengIntern.h"
#include "Object.h"
#include "Resource.h"
#include "System.h"
#include "Scene.h"

class BengSystem;
class BengScene;
class BengObject;

class BengLayer : public BengArray2D<short int> {
    friend class BengObject;
    friend class BengScene;
    friend class BengIGfx;
    friend class BengCamera;

    private:

    int objectsForDeletion_;
    char deleteAll_;

    std::bitset<8> internFlags_;

    void purgeObjects_(void);

    protected:

    BengScene *scene;

    unsigned int tileSizeX;
    unsigned int tileSizeY;

    BengVector offset;
    BengVector scrollSpeed;
    BengVector scrollSpeedAuto;

    std::list<BengObject*> objectList;
    std::list<BengObject*>::iterator objectIterator;

    BengResource<BengISprite> *tileset;

    public:

    BengLayer(unsigned int xsize, unsigned int ysize) : BengArray2D<short int>(xsize, ysize)
    {
        this->offset.x = this->offset.y = this->scrollSpeed.x = this->scrollSpeed.y
        = this->scrollSpeedAuto.x = this->scrollSpeedAuto.y
        = this->objectsForDeletion_ = this->deleteAll_ = 0;

        this->internFlags_.reset();
        this->internFlags_[BENG_LAYER_INTERN_JUST_CREATED]
        = this->internFlags_[BENG_LAYER_INTERN_VISIBLE] = true;

        this->tileset = NULL;
    }
    ~BengLayer(void);

    BengScene *getScene(void);
    BengSystem *getSystem(void);

    void setOffset(BengFixed xIn, BengFixed yIn); // TODO: Implement functionality in real-world
    void getOffset(BengVector &out);
    void setScrollSpeed(BengFixed xIn, BengFixed yIn);
    void getScrollSpeed(BengVector &out);
    void setScrollSpeedAuto(BengFixed xIn, BengFixed yIn);
    void getScrollSpeedAuto(BengVector &out);

    void addObject(BengObject *object);
    void clearObjects(void);
    int getNumObjects(void);

    void setVisibility(bool visibility);
    bool getVisibility(void);

    void getTileSize(unsigned int &xOut, unsigned int &yOut);
    void setTileSize(unsigned int xIn, unsigned int yIn);

    void setTileset(BengResource<BengISprite> *tileset)
    {
        this->tileset = tileset;
    }

    BengResource<BengISprite> *getTileset(void);

    void process(void);

    void destroy(void);

};

#endif
